{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"><li><span><a href=\"#Saving-all-variables-in-a-graph\" data-toc-modified-id=\"Saving-all-variables-in-a-graph-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Saving all variables in a graph</a></span></li><li><span><a href=\"#Restoring-all-variables-from-a-graph\" data-toc-modified-id=\"Restoring-all-variables-from-a-graph-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>Restoring all variables from a graph</a></span></li><li><span><a href=\"#Saving-selected-variables-in-a-graph\" data-toc-modified-id=\"Saving-selected-variables-in-a-graph-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>Saving selected variables in a graph</a></span></li><li><span><a href=\"#Restoring-selected-variables-in-a-graph\" data-toc-modified-id=\"Restoring-selected-variables-in-a-graph-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>Restoring selected variables in a graph</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Saving and Restoring TensorFlow Models <a class=\"tocSkip\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NumPy:1.13.3\n",
      "Pandas:0.21.0\n",
      "Matplotlib:2.1.0\n",
      "TensorFlow:1.4.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Keras:2.0.9\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "import numpy as np\n",
    "np.random.seed(123)\n",
    "print(\"NumPy:{}\".format(np.__version__))\n",
    "\n",
    "import tensorflow as tf\n",
    "tf.set_random_seed(123)\n",
    "print(\"TensorFlow:{}\".format(tf.__version__))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATASETSLIB_HOME = os.path.expanduser('~/dl-ts/datasetslib')\n",
    "import sys\n",
    "if not DATASETSLIB_HOME in sys.path:\n",
    "    sys.path.append(DATASETSLIB_HOME)\n",
    "%reload_ext autoreload\n",
    "%autoreload 2\n",
    "import datasetslib\n",
    "\n",
    "from datasetslib import util as dsu\n",
    "datasetslib.datasets_root = os.path.join(os.path.expanduser('~'),'datasets')\n",
    "models_root = os.path.join(os.path.expanduser('~'),'models')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Saving / Restoring Model in TensorFlow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving all variables in a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved in saved-models/full-graph-save-example.ckpt\n",
      "Values of variables w,b: [ 0.30000001][-0.30000001]\n",
      "output=[ 0.          0.30000001  0.60000002  0.90000004]\n"
     ]
    }
   ],
   "source": [
    "# Saving all variables in a graph\n",
    "\n",
    "tf.reset_default_graph()\n",
    "\n",
    "# Assume Linear Model y = w * x + b\n",
    "# Define model parameters\n",
    "w = tf.Variable([.3], tf.float32)\n",
    "b = tf.Variable([-.3], tf.float32)\n",
    "# Define model input and output\n",
    "x = tf.placeholder(tf.float32)\n",
    "y = w * x + b\n",
    "output = 0\n",
    "\n",
    "# create saver object\n",
    "saver = tf.train.Saver()\n",
    "\n",
    "with tf.Session() as tfs:\n",
    "    # initialize and print the variable y\n",
    "    tfs.run(tf.global_variables_initializer())\n",
    "    output = tfs.run(y,{x:[1,2,3,4]})\n",
    "    saved_model_file = saver.save(tfs,'saved-models/full-graph-save-example.ckpt')\n",
    "    print('Model saved in {}'.format(saved_model_file))\n",
    "    print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))\n",
    "    print('output={}'.format(output))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Restoring all variables from a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from saved-models/full-graph-save-example.ckpt\n",
      "Values of variables w,b: [ 0.30000001][-0.30000001]\n",
      "output=[ 0.          0.30000001  0.60000002  0.90000004]\n"
     ]
    }
   ],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "# Assume Linear Model y = w * x + b\n",
    "# Define model parameters\n",
    "w = tf.Variable([0], dtype=tf.float32)\n",
    "b = tf.Variable([0], dtype=tf.float32)\n",
    "# Define model input and output\n",
    "x = tf.placeholder(dtype=tf.float32)\n",
    "y = w * x + b\n",
    "output = 0\n",
    "\n",
    "# create saver object\n",
    "saver = tf.train.Saver()\n",
    "\n",
    "with tf.Session() as tfs:\n",
    "    saved_model_file = saver.restore(tfs,'saved-models/full-graph-save-example.ckpt')\n",
    "    print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))\n",
    "    output = tfs.run(y,{x:[1,2,3,4]})\n",
    "    print('output={}'.format(output))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving selected variables in a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model saved in saved-models/weights-save-example.ckpt\n",
      "Values of variables w,b: [ 0.30000001][-0.30000001]\n",
      "output=[ 0.          0.30000001  0.60000002  0.90000004]\n"
     ]
    }
   ],
   "source": [
    "# Saving selected variables in a graph in TensorFlow\n",
    "\n",
    "tf.reset_default_graph()\n",
    "\n",
    "# Assume Linear Model y = w * x + b\n",
    "# Define model parameters\n",
    "w = tf.Variable([.3], tf.float32)\n",
    "b = tf.Variable([-.3], tf.float32)\n",
    "# Define model input and output\n",
    "x = tf.placeholder(tf.float32)\n",
    "y = w * x + b\n",
    "output = 0\n",
    "\n",
    "# create saver object\n",
    "saver = tf.train.Saver({'weights': w})\n",
    "\n",
    "with tf.Session() as tfs:\n",
    "    # initialize and print the variable y\n",
    "    tfs.run(tf.global_variables_initializer())\n",
    "    output = tfs.run(y,{x:[1,2,3,4]})\n",
    "    saved_model_file = saver.save(tfs,'saved-models/weights-save-example.ckpt')\n",
    "    print('Model saved in {}'.format(saved_model_file))\n",
    "    print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))\n",
    "    print('output={}'.format(output))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Restoring selected variables in a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from saved-models/weights-save-example.ckpt\n",
      "Values of variables w,b: [ 0.30000001][ 0.]\n",
      "output=[ 0.30000001  0.60000002  0.90000004  1.20000005]\n"
     ]
    }
   ],
   "source": [
    "# Restoring selected variables in a graph in TensorFlow\n",
    "\n",
    "tf.reset_default_graph()\n",
    "\n",
    "# Assume Linear Model y = w * x + b\n",
    "# Define model parameters\n",
    "w = tf.Variable([0], dtype=tf.float32)\n",
    "b = tf.Variable([0], dtype=tf.float32)\n",
    "# Define model input and output\n",
    "x = tf.placeholder(dtype=tf.float32)\n",
    "y = w * x + b\n",
    "output = 0\n",
    "\n",
    "# create saver object\n",
    "saver = tf.train.Saver({'weights': w})\n",
    "\n",
    "with tf.Session() as tfs:\n",
    "    b.initializer.run()\n",
    "    saved_model_file = saver.restore(tfs,'saved-models/weights-save-example.ckpt')\n",
    "    print('Values of variables w,b: {}{}'.format(w.eval(),b.eval()))\n",
    "    output = tfs.run(y,{x:[1,2,3,4]})\n",
    "    print('output={}'.format(output))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": "block",
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
